package com.sinvo.oa.controller;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinvo.oa.entity.model.process.Process;
import com.sinvo.oa.entity.vo.process.ProcessQueryVo;
import com.sinvo.oa.entity.vo.process.ProcessVo;
import com.sinvo.oa.result.R;
import com.sinvo.oa.service.ProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 审批控制器
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping(value = "admin/process")
public class ProcessController {

    @Autowired
    private ProcessService processService;

    /**
     * 分页查询审批
     *
     * @param pageNum        当前页
     * @param pageSize       页大小
     * @param processQueryVo 条件类
     * @return R
     */
    @PreAuthorize("hasAuthority('btn.process.list')")
    @ApiOperation(value = "分页查询")
    @GetMapping("{pageNum}/{pageSize}")
    public R page(@PathVariable Long pageNum, @PathVariable Long pageSize, ProcessQueryVo processQueryVo) {
        Page<ProcessVo> page = new Page<>(pageNum, pageSize);
        page = processService.selectPage(page, processQueryVo);
        return R.success(page);
    }

    /**
     * 查询审批详情
     * @param id processId
     * @return R
     */
    @PreAuthorize("hasAuthority('btn.process.list')")
    @ApiOperation(value = "根据id查询")
    @GetMapping("show/{id}")
    public R show(@PathVariable Long id){
        //根据id获取process
        Process process = processService.getById(id);
        //获取其中 表单值的Json数据
        String formValues = process.getFormValues();
        JSONObject jsonObject = JSON.parseObject(formValues);
        JSONObject formShowData = jsonObject.getJSONObject("formShowData");
        //字符串拼接结果 JSONObject本质是map
        StringBuffer content = new StringBuffer();
        for (Map.Entry entry : formShowData.entrySet()) {
            content.append(entry.getKey()).append("：").append(entry.getValue()).append("\n");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("process",process);
        map.put("formData",content);
        return R.success(map);
    }
}

